package cn.ml.chuji.HuiYuanJiFen;

import java.util.*;

public class JFSYS {
    private final Scanner input = new Scanner(System.in);
    private final List<MyMember> members = new ArrayList<>();
    public void start() {
        while (true) {
            showMenu();
            if(!choseOpts()) break;
        }
    }

    private boolean choseOpts() {
        int n = Integer.parseInt(input.nextLine());
        if(n>6 || n<1){
            System.out.println("输入无效，请重新输入");
            return true;
        }
        if(n == 1){
            saveFen();
        }if(n == 2){
            duiHuan();
        }if(n == 3){
            chaXun();
        }if(n == 4){
            modifyPassword();
        }if(n == 5){
            registerMember();
        }
        return n !=6;//n不是6的话就返回true，n是6返回false
    }

    private void saveFen() {
        MyMember my = inputMember();
        jia(my);
    }

    private void jia(MyMember my) {
        for(MyMember m:members){
            if(m.getCardId().equals(my.getCardId())){
                System.out.println("请输入您此次消费金额（消费1元累计1积分）：");
                int score = Integer.parseInt(input.nextLine());
                m.setFen(m.getFen()+score);
                return;
            }
        }
        System.out.println("您输入的会员卡号或密码错误，无法积累积分！");
    }

    private void duiHuan() {
        MyMember my = inputMember();
        huan(my);
    }

    private void huan(MyMember my) {
        for(MyMember m:members){
            if(m.getCardId().equals(my.getCardId())){
                System.out.println("请输入您需要兑换使用的积分（100积分抵用0.1元，不足100的积分不做抵换）");
                int score=Integer.parseInt(input.nextLine());//输入想要兑换的积分数
                int you = m.getFen();//实际有的积分
                if(you >= score){
                    m.setFen(you-score);
                    System.out.println("您的消费金额中使用积分抵消"+score/100*0.1+"元");
                }else {
                    System.out.println("抱歉，您的积分不够，无法抵用消费金额");
                }
                return;
            }
        }
        System.out.println("您输入的会员卡号或密码错误，无法完成积分兑换！");
    }

    private void chaXun() {
        MyMember my = inputMember();
        showMy(my);
    }

    private void showMy(MyMember my) {
        for(MyMember m:members){
            if(m.getCardId().equals(my.getCardId())){
                System.out.println(m);
                return;
            }
        }
        System.out.println("您输入的会员卡号或密码错误，无法查询积分！");
    }

    private void modifyPassword() {
        //老的
        MyMember my = inputMember();
        if(hasMember(my)){
            gai(my);
        //改新的
        }else {
            System.out.println("您输入的会员卡号或密码错误，无法修改密码！");
        }
    }

    private MyMember inputMember() {
        System.out.println("请输入您的会员卡号：");
        String cardId = input.nextLine();
        System.out.println("请输入您的会员密码：");
        String pwd = genPwd();
        MyMember my = new MyMember(cardId,pwd);
        return my;
    }

    private void gai(MyMember my) {
        for(MyMember m:members){
            if(m.getCardId().equals(my.getCardId())){
                String pwd = genPwd();
                m.setPassword(pwd);
                System.out.println(m);
                return;
            }
        }
    }

    private boolean hasMember(MyMember my) {
        for(MyMember m:members){
            if(m.getCardId().equals(my.getCardId())){
                return true;
            }
        }
        return false;
    }

    private void registerMember() {
        System.out.println("请输入注册姓名");
        String cardId = genCardId();//初始id
        String name = input.nextLine();
        System.out.println("请输入注册密码");
        String password = genPwd();
        int fen = 100;
        MyMember my = new MyMember(name,cardId,password,fen);
        members.add(my);
        System.out.println(my);
    }
    //初始密码
    private String genPwd() {
        String pwd;
        while (true){
            pwd = input.nextLine();
            if(pwd.length() >= 6) break;
            System.out.println("会员密码不能小于6位，请重新输入：");

        }
      return pwd;
    }
    //初始ID
    private String genCardId() {
        boolean flag=true;
        Random random = new Random();
        int id = random.nextInt(999999);
        Set<String> set = new HashSet<>();
        for(MyMember member:members){
            set.add(member.getCardId());
        }
        while (true){
            id=random.nextInt(99999999);
            if(set.add(id+"")) break;

        }
//        for(MyMember member:members){
//            if(member.getCardId().equals(id+"")){
//                id=random.nextInt(99999999);
//            }
//        }
        return id+"";
    }

    private void showMenu() {
        System.out.println("**************************欢迎进入超市会员管理系统**************************");
        System.out.println("1.积分累计    2.积分兑换   3.查询剩余积分   4.修改密码   5.开卡   6.退出");
        System.out.println("****************************************************************************");
        System.out.println("请选择：");
    }
}
